package cn.monkey.family.server.domain.shop.item.type;

import cn.monkey.family.data.Result;
import cn.monkey.family.data.Results;
import cn.monkey.family.data.crud.AbstractCrudService;
import cn.monkey.family.data.shop.item.type.*;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class ItemTypeService extends AbstractCrudService<String, ItemTypeQueryRequest, ItemType, ItemTypeDto, ItemTypeVo, ItemTypeMapper, ItemTypeRepository>
        implements IItemTypeService {
    public ItemTypeService(ItemTypeRepository repository, ItemTypeMapper mapper) {
        super(repository, mapper);
    }

    @Override
    public Result<Collection<ItemTypeVo>> readTree(ItemTypeQueryRequest queryRequest, Sort sort, Integer maxDeep) {
        List<ItemType> itemTypeList = this.repository.findAllByQueryRequest(queryRequest, sort);
        if (CollectionUtils.isEmpty(itemTypeList)) {
            return Results.ok(Collections.emptyList());
        }
        return Results.ok(this.mapper.toTree(itemTypeList, maxDeep));
    }

    @Override
    public Result<Collection<String>> distinctKey(ItemTypeQueryRequest queryRequest, Sort sort) {
        List<ItemType> itemTypeList = this.repository.findAllByQueryRequest(queryRequest, sort);
        if (CollectionUtils.isEmpty(itemTypeList)) {
            return Results.ok(Collections.emptyList());
        }
        return Results.ok(itemTypeList.stream().map(ItemType::getKey).toList());
    }
}
